/**
 * @@company YIMAFU Co.
 * @copyright Copyright @2014, YIMAFU Co., Ltd. All right.
 * @version：1.0
 **/
package cn.com.qingz.core.utils;

import java.util.List;

/**
 * @author lifajun
 * @date 2013年7月11日 下午4:18:21
 * @Description:
 * 
 */
public class PageResult {

	/**
	 * 页数，默认是第一页
	 */
	private Integer page;
	/**
	 * 每页显示记录条数，默认10条
	 */
	private Integer limit;
	/**
	 * 总记录数
	 */
	private Integer totalCount;
	/**
	 * 记录
	 */
	private List<?> list;
	/**
	 * 分页条
	 */
	private String pager;

	/**
	 * 是否第一页
	 */
	// private boolean firstPage;
	/**
	 * 是否最后一页
	 */
	// private boolean lastPage;
	/**
	 * 上一页页码
	 */
	// private Integer prePage;
	/**
	 * 下一页页码
	 */
	// private Integer nextPage;
	/**
	 * 是否有上一页
	 */
	// private boolean hasPrePage;
	/**
	 * 是否有下一页
	 */
	// private boolean hasNextPage;
	/**
	 * 总页数
	 */
	// private Integer totalPages;

	/**
	 * 是否是首页（第一页），第一页页码为1
	 * 
	 * @return 首页标识
	 */
	public boolean isFirstPage() {
		return page <= 1;
	}

	/**
	 * 是否是最后一页
	 * 
	 * @return 末页标识
	 */
	public boolean isLastPage() {
		return page >= getTotalPages();
	}

	public int getPrePage() {
		if (isHasPrePage()) {
			return page - 1;
		} else {
			return page;
		}
	}

	public int getNextPage() {
		if (isHasNextPage()) {
			return page + 1;
		} else {
			return page;
		}
	}

	/**
	 * 是否有上一页
	 * 
	 * @return 上一页标识
	 */
	public boolean isHasPrePage() {
		return (page - 1 >= 1);
	}

	/**
	 * 是否有下一页
	 * 
	 * @return 下一页标识
	 */
	public boolean isHasNextPage() {
		return (page + 1 <= getTotalPages());
	}

	/**
	 * 得到 总页数
	 * 
	 * @return
	 */
	public int getTotalPages() {
		if (totalCount <= 0) {
			return 0;
		}
		if (limit <= 0) {
			return 0;
		}

		int count = totalCount / limit;
		if (totalCount % limit > 0) {
			count++;
		}
		return count;
	}

	/**
	 * 取得总项数。
	 * 
	 * @return 总项数
	 */
	public int getTotalCount() {
		return totalCount;
	}

	/**
	 * @return the pager
	 */
	public String getPager() {
		StringBuffer sb = new StringBuffer();
		if (this.getTotalCount() > 0) {
			sb.append(" <ul class=\"pager\">\n");
			if (this.getPage() == 1) {
				sb.append(" <li class=\"disabled\"><a href=\"#\">首页</a></li>\n");
				sb.append(" <li class=\"disabled\"><a href=\"#\">上一页</a></li>\n");
			} else {
				sb.append(" <li><a href=\"#\" onclick=\"nextPage(1)\">首页</a></li>\n");
				sb.append(" <li><a href=\"#\" onclick=\"nextPage(" + this.getPrePage() + ")\">上一页</a></li>\n");
			}

			if (this.getPage() == this.getTotalPages()) {
				sb.append(" <li class=\"disabled\"><a href=\"#\">下一页</a></li>\n");
				sb.append(" <li class=\"disabled\"><a href=\"#\">尾页</a></li>\n");
			} else {
				sb.append(" <li><a href=\"#\" onclick=\"nextPage(" + this.getNextPage() + ")\">下一页</a></li>\n");
				sb.append(" <li><a href=\"#\" onclick=\"nextPage(" + this.getTotalPages() + ")\">尾页</a></li>\n");
			}
			// sb.append(" <li>第" + this.getPage() + "页</li>\n");
			// sb.append(" <li>共" + this.getTotalPages() + "页</li>\n");
			sb.append("</ul>\n");
			sb.append("<script type=\"text/javascript\">\n");
			sb.append("function nextPage(page){\n");
			// sb.append(" if(true && document.forms[0]){\n");
			// sb.append("     var url = document.forms[0].getAttribute(\"action\");\n");
			// sb.append("     if(url.indexOf('?')>-1){url += \"&page=\";}\n");
			// sb.append("     else{url += \"?page=\";}\n");
			// sb.append("     document.forms[0].action = url+page;\n");
			// sb.append("     document.forms[0].submit();\n");
			// sb.append(" }else{\n");
			sb.append("     var url = document.URL;\n");
			sb.append("     url = url.replace('#', '');\n");
			sb.append("     if(url.indexOf('?')>-1){\n");
			sb.append("         if(url.indexOf('page')>-1){\n");
			sb.append("             var reg = /page=\\d*/g;\n");
			sb.append("             url = url.replace(reg,'page=');\n");
			sb.append("         }else{\n");
			sb.append("             url += \"&page=\";\n");
			sb.append("         }\n");
			sb.append("     }else{url += \"?page=\";}\n");
			sb.append("     document.location = url + page;\n");
			// sb.append(" }\n");
			sb.append("     return false;\n");
			sb.append("}\n");
			sb.append("</script>\n");
		}
		pager = sb.toString();
		return pager;
	}

	/**
	 * @param pager
	 *            the pager to set
	 */
	public void setPager(String pager) {
		this.pager = pager;
	}

	/**
	 * @return the page
	 */
	public Integer getPage() {
		return page;
	}

	/**
	 * @param page
	 *            the page to set
	 */
	public void setPage(Integer page) {
		this.page = page;
	}

	/**
	 * @return the limit
	 */
	public Integer getLimit() {
		return limit;
	}

	/**
	 * @param limit
	 *            the limit to set
	 */
	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	/**
	 * @return the list
	 */
	public List<?> getList() {
		return list;
	}

	/**
	 * @param list
	 *            the list to set
	 */
	public void setList(List<?> list) {
		this.list = list;
	}

	/**
	 * @param firstPage
	 *            the firstPage to set
	 */
	// public void setFirstPage(boolean firstPage) {
	// this.firstPage = firstPage;
	// }

	/**
	 * @param lastPage
	 *            the lastPage to set
	 */
	// public void setLastPage(boolean lastPage) {
	// this.lastPage = lastPage;
	// }

	/**
	 * @param hasPrePage
	 *            the hasPrePage to set
	 */
	// public void setHasPrePage(boolean hasPrePage) {
	// this.hasPrePage = hasPrePage;
	// }

	/**
	 * @param hasNextPage
	 *            the hasNextPage to set
	 */
	// public void setHasNextPage(boolean hasNextPage) {
	// this.hasNextPage = hasNextPage;
	// }

	/**
	 * @param totalCount
	 *            the totalCount to set
	 */
	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	/**
	 * @param prePage
	 *            the prePage to set
	 */
	// public void setPrePage(Integer prePage) {
	// this.prePage = prePage;
	// }

	/**
	 * @param totalPages
	 *            the totalPages to set
	 */
	// public void setTotalPages(Integer totalPages) {
	// this.totalPages = totalPages;
	// }

	/**
	 * @param nextPage
	 *            the nextPage to set
	 */
	// public void setNextPage(Integer nextPage) {
	// this.nextPage = nextPage;
	// }

}
